package hierarchydisplay;

import java.awt.Component;
import java.awt.Graphics;

/* The generic component to display hierarchies as a tree of nodes. */
public class HierarchyDisplay {

	/* State variables */
	protected Component view = null;
	
	/* Flag if layout is valid */
	protected boolean validLayout = false; 

	protected Node root;
	
	/* Refresh display */
	public void refresh() {
		view.repaint();
	}
		
	/* Update hierarchy layout */
	public void calculateLayout() {
		if (getRoot() != null) {
			getRoot().calculateLayout(50, 100, 0, view.getWidth());
		}
		validLayout = true;
	}
	
	/* Draw the hierarchy */
	public void draw(Graphics g) {
		calculateLayout();
		
		if (getRoot() != null) {
			getRoot().draw(g);
		}
	}
	
	/* Set view, where hierarchy is diplayed */
	public void setView(Component view) {
		this.view = view;
	}
	
	/* Get graphics */
	public Graphics getGraphics() {
		return view.getGraphics();
	}
	
	/* invalidate parent layout */
	public void invalidateLayout() {
		validLayout = false;
	}

	/* set root node */
	public void setRoot(Node root) {
		this.root = root;
	}
	
	/* get root node */
	public Node getRoot() {
		return root;
	}

}
